package com.kyzh.core.activities

import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.google.android.material.appbar.AppBarLayout
import com.gushenge.atools.util.AView
import com.gushenge.core.base.Base.BASEURL
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.ProductDetail
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.impls.UserImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.dialog.showGetPointDialog
import com.kyzh.core.dialog.showToVerifyDialog
import com.kyzh.core.utils.checkLogin
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast
import com.kyzh.core.pager.login.LoginActivity
import com.kyzh.core.utils.ViewUtils
import kotlinx.android.synthetic.main.activity_product_new.*
import kotlinx.android.synthetic.main.activity_product_new.appbar
import kotlinx.android.synthetic.main.activity_product_new.toolbar
import kotlinx.android.synthetic.main.activity_product_new.topbg
import kotlinx.android.synthetic.main.activity_product_new.tvName
import org.jetbrains.anko.backgroundColorResource
import org.jetbrains.anko.dip
import org.jetbrains.anko.toast


class ProductDetailActivity : BaseActivity() {

    private val context = this@ProductDetailActivity
    lateinit var id: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_new)
        initView()
    }

    private fun initView() {
        AView.setStatusBar(this, true)
        AView.setMargins(toolbar, 0, AView.getStatusBarHeight(this), 0, 0)
        AView.setMargins(topbg, 0, AView.getStatusBarHeight(this) + dip(45), 0, 0)
//        ViewUtils.setHeight(appbar, dip(270) + AView.getStatusBarHeight(this))

        id = intent.getStringExtra(GlobalKeys.KEY) ?: "0"
        WealRequest.productDetail(id) {
            var bean = this
            Glide.with(context).load(bean.goods_thumb).into(ivThumb)
            tvName.text = bean.goods_name
            pbNum.progress = bean.bfb
            tvNum.text = "${getString(R.string.remainNum)}: ${bean.goods_number}"

            tvDesc.webViewClient = WebViewClient()
            tvDesc.loadUrl("${BASEURL}/?ct=azmall&ac=goods_jieshao&app=1&id=${id}")
            btGet.setOnClickListener {
                if (checkLogin()) {
                    WealRequest.selectProduct(bean.goods_id) {
                        when (it) {
                            1 -> {
                                startActivity<OrderDoneActivity>(GlobalKeys.KEY to bean)
                            }
                            2 -> {
                                showGetPointDialog()
                            }
                            3 -> {
                                showToVerifyDialog()
                            }
                        }
                    }
                } else {
                    startActivity<LoginActivity>()
                }
            }
            closeImg.setOnClickListener {
                finish()
            }
            appbar.addOnOffsetChangedListener(object : ViewUtils.AppBarLayoutStateChangeListener() {
                override fun onStateChanged(appBarLayout: AppBarLayout, state: State) {
                    when (state) {
                        State.EXPANDED -> {
                            AView.setStatusBar(this@ProductDetailActivity, false)
                            tvTitle.text = "商品详情"
                        }

                        State.COLLAPSED -> {
                            AView.setStatusBar(this@ProductDetailActivity, true)
                            tvTitle.text = bean.goods_name
                        }
                        State.INTERMEDIATE -> {
                            AView.setStatusBar(this@ProductDetailActivity, true)
                           tvTitle.text = ""
                            toolbar.backgroundColorResource = android.R.color.transparent

                        }
                    }
                }
            })
            btCollect.setOnClickListener {
                if (bean.is_shoucang == 0) {
                    UserImpl.collect(id, 1, object : ResultListener {
//                        override fun success() {
//                            getString(R.string.collectSuccess).toast()
//                            bean.is_shoucang=1
//                            refreshCollect(bean)
//                        }

                        override fun success(bean1: Any) {
                            getString(R.string.collectSuccess).toast()
                            bean.is_shoucang = 1
                            refreshCollect(bean)
                        }

                        override fun error(error: String) {
                            toast(error)
                        }
                    })
                } else {
                    WealRequest.collectCancel(id, 1) {
                        bean.is_shoucang = 0
                        refreshCollect(bean)
                    }
                }
            }
            refreshCollect(bean)

        }
        tvDesc.setOnTouchListener { v, _ ->
            (v as WebView).requestDisallowInterceptTouchEvent(true)
            false
        }
    }

    private fun refreshCollect(bean: ProductDetail) {
        btCollect.setCompoundDrawablesWithIntrinsicBounds(
            null,
            ContextCompat.getDrawable(
                this@ProductDetailActivity,
                if (bean.is_shoucang == 1) R.drawable.ic_collect else R.drawable.ic_collect_no
            ),
            null, null
        )
    }
}
